package 数学;
import java.util.*;

public class rotate {
    public static void rotateM(int[][] matrix){
        // 先对角线翻转，再左右翻转
        int m =  matrix.length;
        int n = matrix[0].length;
        // 对角线翻转
        for(int i = 0; i < m; i++){
            // 从i开始，因为对角线翻转，只需要遍历一半的元素
            for(int j = i; j < n; j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        // 左右翻转
        for(int i = 0; i < m; i++){
            // 从0开始，因为左右翻转，只需要遍历一半的元素
            int left = 0, right = n - 1;
            while(left < right){
                int temp = matrix[i][left];
                matrix[i][left] = matrix[i][right];
                matrix[i][right] = temp;
                left++;
                right--;
            }
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] matrix = new int[m][n];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                matrix[i][j] = sc.nextInt();
            }
        }
        // deepToString方法作用：将二维数组转换为字符串
        System.out.println(Arrays.deepToString(matrix));
        rotateM(matrix);
        System.out.println(Arrays.deepToString(matrix));
        sc.close();
    }
}
